﻿using System;
using UnityEngine;

namespace ETModel {
    public static class ConfigHelper {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configFileInfo">bundleName|prefabName</param>
        /// <param name="key"></param>
        /// <returns></returns>
		public static string GetText(string configFileInfo, string key) {
            if (string.IsNullOrEmpty(configFileInfo)) throw new Exception("参数configFileInfo不能为空");
            var cArgs = configFileInfo.Split('|');
            if (cArgs.Length != 2) throw new Exception("参数configFileInfo格式不正确,应为bundleName|prefabName ");
            var bundleName = cArgs[0];
            var prefabName = cArgs[1];

            try {

                GameObject config = (GameObject)Game.Scene.GetComponent<ResourcesComponent>().GetAsset(bundleName, prefabName);
                string configStr = config.Get<TextAsset>(key).text;
                return configStr;
            } catch (Exception e) {
                throw new Exception($"load config file fail,configFileInfo:{configFileInfo}, key: {key}", e);
            }
        }

        public static string GetGlobal() {
            try {
                GameObject config = (GameObject)ResourcesHelper.Load("KV");
                string configStr = config.Get<TextAsset>("GlobalProto").text;
                return configStr;
            } catch (Exception e) {
                throw new Exception($"load global config file fail", e);
            }
        }

        public static T ToObject<T>(string str) {
            return JsonHelper.FromJson<T>(str);
        }
    }
}